package com.bins.dentist.clinic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bins.dentist.clinic.constants.CommonConstants;
import com.bins.dentist.clinic.entity.AppointmentEntity;
import com.bins.dentist.clinic.mapper.AppointmentMapper;
import com.bins.dentist.clinic.model.dto.AppointmentDto;
import com.bins.dentist.clinic.model.dto.PatientPageDto;
import com.bins.dentist.clinic.model.vo.AppointmentVo;
import com.bins.dentist.clinic.model.vo.SimpleSelectVo;
import com.bins.dentist.clinic.service.AppointmentService;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 预约表 服务实现类
 * </p>
 *
 * @author bins
 * @since 2023-04-23 13:22:36
 */
@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, AppointmentEntity> implements AppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public IPage<AppointmentVo> getPage(PatientPageDto pageDto) {
        LambdaQueryWrapper<AppointmentEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(!ObjectUtils.isEmpty(pageDto.getMedicalRecordNo()), AppointmentEntity::getMedicalRecordNo, pageDto.getMedicalRecordNo())
                .like(!ObjectUtils.isEmpty(pageDto.getPatientName()), AppointmentEntity::getPatientName, pageDto.getPatientName())
                .eq(!ObjectUtils.isEmpty(pageDto.getMobile()), AppointmentEntity::getMobile, pageDto.getMobile())
                .eq(!ObjectUtils.isEmpty(pageDto.getMedicalRecordNo()), AppointmentEntity::getMedicalRecordNo, pageDto.getMedicalRecordNo())
                .eq(AppointmentEntity::getIsDel, CommonConstants.DELETED_NO);
        IPage page = appointmentMapper.selectPage(new Page(pageDto.getPageNum(), pageDto.getPageSize()), queryWrapper);
        List<AppointmentEntity> pageList = page.getRecords();
        if (CollectionUtils.isEmpty(pageList)) {
            return page;
        }
        List<AppointmentVo> dateList = Lists.newArrayListWithCapacity(pageList.size());
        for (AppointmentEntity item : pageList) {
            AppointmentVo itemVo = new AppointmentVo();
            BeanUtils.copyProperties(item, itemVo);
            dateList.add(itemVo);
        }
        page.setRecords(dateList);
        return page;
    }

    @Override
    public AppointmentVo getDetail(AppointmentDto dto) {
        AppointmentEntity item = appointmentMapper.selectById(dto.getId());
        AppointmentVo resultVo = new AppointmentVo();
        BeanUtils.copyProperties(item, resultVo);
        return resultVo;
    }

    @Override
    public AppointmentVo saveItem(AppointmentDto dto) {
        AppointmentEntity item = new AppointmentEntity();
        BeanUtils.copyProperties(dto, item);
        item.setUpdateBy(0l);
        item.setUpdateTime(LocalDateTime.now());
        if (ObjectUtils.isEmpty(dto.getId())) {
            item.setIsDel(CommonConstants.DELETED_NO);
            item.setCreateBy(0l);
            item.setCreateTime(LocalDateTime.now());
            appointmentMapper.insert(item);
        } else {
            appointmentMapper.updateById(item);
        }
        AppointmentVo resultVo = new AppointmentVo();
        resultVo.setId(item.getId());
        return resultVo;
    }

    @Override
    public Integer deleteItem(AppointmentDto dto) {
        AppointmentEntity existItem = appointmentMapper.selectById(dto.getId());
        AppointmentEntity item = new AppointmentEntity();
        item.setId(dto.getId());
        item.setIsDel(CommonConstants.DELETED_YES);
        return appointmentMapper.updateById(item);
    }

    @Override
    public List<SimpleSelectVo> selectList(AppointmentDto dto) {
        LambdaQueryWrapper<AppointmentEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(AppointmentEntity::getIsDel, CommonConstants.DELETED_NO);
        List<AppointmentEntity> queryList = appointmentMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(queryList)) {
            return Collections.emptyList();
        }
        List<SimpleSelectVo> resultList = Lists.newArrayListWithCapacity(queryList.size());
        SimpleSelectVo simpleSelectVo;
        for (AppointmentEntity item : queryList) {
            simpleSelectVo = new SimpleSelectVo();
            simpleSelectVo.setId(item.getId());
            simpleSelectVo.setName(item.getPatientName());
            resultList.add(simpleSelectVo);
        }
        return resultList;
    }
}
